package utils

import "time"

func Date(tm time.Time) int64 {

	year, month, day := tm.Date()
	return time.Date(year, month, day, 0, 0, 0, 0, time.Local).Unix()
}

func DateToUnix(date string, format string) int64 {
	theTime, _ := time.ParseInLocation(format, date, time.Local)
	return theTime.Unix()
}

func UnixToDate(ts int64, format string) string {
	return time.Unix(ts, 0).Format(format)
}

func FirstDayOfMonth(month string, layout string) int {

	theTime, _ := time.ParseInLocation(layout, month, time.Local)
	currentYear, currentMonth, _ := theTime.Date()
	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, time.Local)

	return firstOfMonth.Day()
}

func LastDayOfMonth(month string, layout string) int {

	theTime, _ := time.ParseInLocation(layout, month, time.Local)
	currentYear, currentMonth, _ := theTime.Date()
	currentLocation := theTime.Location()
	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)

	return lastOfMonth.Day()
}

func FirstAndLastUnixOfMonth(month string, layout string) (s int64, e int64) {

	theTime, _ := time.ParseInLocation(layout, month, time.Local)
	currentYear, currentMonth, _ := theTime.Date()
	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, time.Local)
	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)

	return firstOfMonth.Unix(), lastOfMonth.Unix()
}

func FirstAndLastUnixOfYear(month string, layout string) (s int64, e int64) {

	theTime, _ := time.ParseInLocation(layout, month, time.Local)
	currentYear, _, _ := theTime.Date()
	firstOfYear := time.Date(currentYear, 1, 1, 0, 0, 0, 0, time.Local)
	lastOfYear := firstOfYear.AddDate(1, 0, -1)

	return firstOfYear.Unix(), lastOfYear.Unix()
}

func AddMonthForUnix(month string, layout string, num int) int64 {
	theTime, _ := time.ParseInLocation(layout, month, time.Local)
	year := theTime.AddDate(0, num, 0)
	return year.Unix()
}
